<?php

namespace app\api\controller;

use app\admin\model\old\order\Aftersale;
use think\Db;
use think\exception\PDOException;
use app\common\controller\Api;
use think\Validate;

/**
 * 示例接口
 */
class OldOrder extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * [refund 退款]
     * @since  2022-10-10
     * @return [type]     [description]
     */
    public function refund()
    {
        $phone = $this->request->post("phone"); // 手机号
        $money = $this->request->post("money"); //
        $images =  $this->request->post("images/a"); // 图片
        $desc = $this->request->post("desc");//退款理由
        // $bitMax = 2*1024*1024;
        // $bitMax = 2;
        if (empty($desc) ) {
            $this->error(__('desc can not be empty'));
        }
        if (empty(floatval($money)) ) {
            $this->error(__('money can not be empty'));
        }

        if (!Validate::regex($phone, "^1[3-9][0-9]\d{8}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        // $this->auth->id = 1;
        if (Aftersale::where('user_id','=',$this->auth->id)->find()) {
            $this->error(__('Return already exists'));
        }
        $img = [];
        if (is_array($images)) {
            foreach ($images as $value) {
                if (is_string($value)) {
                    $img[] = $value;
                }
            }
        }
        $option = [
            'user_id'=>$this->auth->id,
            'aftersale_sn'=>Aftersale::getSn($this->auth->id),
            'phone'=>$phone,
            'type'=>'refund',
            'fee'=>floatval($money),
            'images'=>json_encode($img),
            'desc'=>$desc,
            'refund_status'=>Aftersale::REFUND_STATUS_0,
            'createtime'=>time(),
        ];
        
        (new Aftersale)->save($option);
        $this->success();
        
    }
    public function list()
    {
        // $this->auth->id =1;
        $limit = $this->request->param("limit",10);//退款理由
        $data = (new Aftersale)
                ->where('user_id','=',$this->auth->id)
                // ->json(['images'])
                ->order('id desc')
                ->paginate($limit)->toArray();
        foreach ($data['data'] as $key => $value) {
            
            $data['data'][$key]['images'] = json_decode($value['images'],true);
        }
        $this->success('成功',$data);
    }
}
